import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";

import DefaultLayout from "@/components/DefaultLayout.vue";

const routes: RouteRecordRaw[] = [
    {
        path: '/', component: DefaultLayout, children: [
            {path: '/dashboard', component: () => import('@pages/Dashboard.vue')},
            {
                path: '/blog-edit',
                name: 'blog-edit',
                props: route => ({mode: 'edit', blog_id: parseInt(route.query.blog_id as string)}),
                component: () => import('@pages/blog/BlogEdit.vue')
            },
            {
                path: '/blog-add',
                name: 'blog-add',
                props: () => ({mode: 'add'}),
                component: () => import('@pages/blog/BlogEdit.vue')
            },
            {path: '/blog-list', name: 'blog-list', component: () => import('@pages/blog/BlogList.vue')},
            {path: '/blog-category', name: 'blog-category', component: () => import('@pages/blog/BlogCategory.vue')},
            {path: '/blog-comment', name: 'blog-comment', component: () => import('@pages/blog/BlogComment.vue')},
        ]
    },
    {path: '/login', component: () => import('@pages/Login.vue')}
]


export const router = createRouter({
    routes,
    history: createWebHashHistory('/admin')
})

router.beforeEach((to, _, next) => {
    if (to.path === '/login') {
        next()
    } else {
        if (sessionStorage.getItem('token')) {
            next()
        } else {
            next('/login')
        }
    }
})